﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" Inherits="BasePermissionPage" %>

<%@ Register Src="~/UserControls/ucDateTimePicker.ascx" TagName="ucDateTimePicker" TagPrefix="Kerry" %>
<%@ Register Src="~/UserControls/ucGridViewPager.ascx" TagName="ucGridViewPager" TagPrefix="Kerry" %>
<%@ Register Src="~/UserControls/ucFavorite.ascx" TagName="ucFavorite" TagPrefix="Kerry" %>

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      txt_START_DATE.Text = DateTime.Now.ToString("yyyyMMdd");
      txt_END_DATE.Text = DateTime.Now.ToString("yyyyMMdd");

      ////載入每一頁最後查詢條件資料
      //string pageUrl = Request.AppRelativeCurrentExecutionFilePath;
      //if (Profile.PageConditionRecord != null)
      //{
      //  List<ControlItem> ControlItems;
      //  if (Profile.PageConditionRecord.PageItem.TryGetValue(pageUrl, out ControlItems))
      //  {
      //    var allControls = ControlUtil.FindControlsOfType<WebControl>(Page);
      //    var ConditionControls = ControlItems.Select(c => c.ControlID.ToLower()).ToArray();
      //    var ConditionMatchControls = allControls.Where(c => ConditionControls.Contains(c.ID.ToLower()));
      //    foreach (var matchControl in ConditionMatchControls)
      //    {
      //      var record = ControlItems.Where(c => c.ControlID.ToLower() == matchControl.ID.ToLower()).SingleOrDefault();
      //      if (record != null)
      //      {
      //        if (matchControl is TextBox)
      //        {
      //          (matchControl as TextBox).Text = record.ControlValue;
      //        }
      //        if (matchControl is DropDownList)
      //        {
      //          (matchControl as DropDownList).SelectedValue = record.ControlValue;
      //        }
      //      }
      //    }
      //  }
      //}
    }
  }
  protected void SDS_KRJ_BULLETIN_MASTER_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
  {
    //第一次瀏覽頁面時，不秀資料
    if (!Page.IsPostBack)
    {
      e.Cancel = true;
    }
  }

  protected void BindGrid()
  {
    SDS_KRJ_BULLETIN_MASTER.SelectParameters.Clear();

    //設定查詢條件
    if (!string.IsNullOrEmpty(txt_BULLETIN_ID.Text))
    {
      SDS_KRJ_BULLETIN_MASTER.SelectCommand += " AND BULLETIN_ID = :BULLETIN_ID ";
      SDS_KRJ_BULLETIN_MASTER.SelectParameters.Add(":BULLETIN_ID", txt_BULLETIN_ID.Text);
    }
    if (!string.IsNullOrEmpty(txt_BULLETIN_TEXT.Text))
    {
      string lang = ((Label)Master.FindControl("lbl_Language")).Text;
      switch (lang)
      {
        case "zh-TW":
          SDS_KRJ_BULLETIN_MASTER.SelectCommand += " AND BULLETIN_TEXT_TW LIKE :BULLETIN_TEXT_TW ";
          SDS_KRJ_BULLETIN_MASTER.SelectParameters.Add(":BULLETIN_TEXT_TW", "%" + txt_BULLETIN_TEXT.Text + "%");
          break;
        case "zh-CN":
          SDS_KRJ_BULLETIN_MASTER.SelectCommand += " AND BULLETIN_TEXT_GB LIKE :BULLETIN_TEXT_GB ";
          SDS_KRJ_BULLETIN_MASTER.SelectParameters.Add(":BULLETIN_TEXT_GB", "%" + txt_BULLETIN_TEXT.Text + "%");
          break;
        case "en-US":
          SDS_KRJ_BULLETIN_MASTER.SelectCommand += " AND BULLETIN_TEXT_EN LIKE :BULLETIN_TEXT_EN ";
          SDS_KRJ_BULLETIN_MASTER.SelectParameters.Add(":BULLETIN_TEXT_EN", "%" + txt_BULLETIN_TEXT.Text + "%");
          break;
      }
    }
    if (!string.IsNullOrEmpty(txt_START_DATE.Text) && !string.IsNullOrEmpty(txt_END_DATE.Text))
    {
      SDS_KRJ_BULLETIN_MASTER.SelectCommand += " AND (START_DATE <= :START_DATE AND END_DATE >= :END_DATE) ";
      SDS_KRJ_BULLETIN_MASTER.SelectParameters.Add(":START_DATE", txt_START_DATE.Text);
      SDS_KRJ_BULLETIN_MASTER.SelectParameters.Add(":END_DATE", txt_END_DATE.Text);
    }
    //強制執行查詢
    SDS_KRJ_BULLETIN_MASTER.Select(DataSourceSelectArguments.Empty);
    GridView_BULLETIN.DataBind();
  }

  protected void btn_Query_Click(object sender, EventArgs e)
  {
    //START：紀錄每一頁最後查詢條件資料 by Adams 2013/6/26
    try
    {
      var controlItem = new List<ControlItem>() 
    {
      new ControlItem("txt_BULLETIN_ID", txt_BULLETIN_ID.Text, txt_BULLETIN_ID.GetType()),
      new ControlItem("txt_BULLETIN_TEXT", txt_BULLETIN_TEXT.Text, txt_BULLETIN_TEXT.GetType()),
      new ControlItem("txt_START_DATE", txt_START_DATE.Text, txt_START_DATE.GetType()),
      new ControlItem("txt_END_DATE", txt_END_DATE.Text, txt_END_DATE.GetType())
    };
      ConditionRecordUtil.RecordSearchConditionData(controlItem);
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayInfoMessage(ex.Message, lbl_message);
    }
    
    BindGrid();

    if (GridView_BULLETIN.Rows.Count == 0)
    {
      CommonUtil.DisplayErrorMessage("查無資料", lbl_message);
    }
  }
  
  protected void SDS_KRJ_BULLETIN_MASTER_Deleted(object sender, SqlDataSourceStatusEventArgs e)
  {
    if (e.Exception == null)
    {
      CommonUtil.DisplayInfoMessage("刪除成功", lbl_message);
    }
    else
    {
      e.ExceptionHandled = true;
      CommonUtil.DisplayErrorMessage(e.Exception.Message, lbl_message);
    }
  }

  protected void GridView_BULLETIN_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
  {
    string id = GridView_BULLETIN.Rows[e.NewSelectedIndex].Cells[1].Text;
    Response.Redirect("~/Announcement/AnnouncementDetail.aspx?mode=view&bid=" + id);
  }

  protected void btn_Insert_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/Announcement/AnnouncementDetail.aspx?mode=add");
  }

  protected void GridView_BULLETIN_RowEditing(object sender, GridViewEditEventArgs e)
  {
    string id = GridView_BULLETIN.Rows[e.NewEditIndex].Cells[1].Text;
    Response.Redirect("~/Announcement/AnnouncementDetail.aspx?mode=edit&bid=" + id);
  }

  protected void GridView_BULLETIN_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    string id = GridView_BULLETIN.Rows[e.RowIndex].Cells[1].Text;
    Response.Redirect("~/Announcement/AnnouncementDetail.aspx?mode=delete&bid=" + id);
  }

  protected void GridView_BULLETIN_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    GridView_BULLETIN.PageIndex = e.NewPageIndex;
    BindGrid();
  }
</script>


<asp:Content runat="server" ID="content1" ContentPlaceHolderID="MainContent">

  <div>
    <table class="ConditionTable">
      <th colspan="2">
        <Kerry:ucFavorite ID="ucFavorite" runat="server" />
        公告維護
      </th>
      <tr>
        <td>公告代號：<asp:TextBox ID="txt_BULLETIN_ID" runat="server" />
        </td>
        <td>公告大綱：<asp:TextBox ID="txt_BULLETIN_TEXT" runat="server" />
        </td>
      </tr>
      <tr>
        <td>有效日期 : 
           <%--<Kerry:ucDateTimePicker ID="txt_START_DATE" runat="server" />--%>
          <asp:TextBox ID="txt_START_DATE" runat="server"></asp:TextBox>
          <ajaxToolkit:CalendarExtender ID="txt_START_DATE_CalendarExtender" runat="server"
            Enabled="True" Format="yyyyMMdd" PopupButtonID="txt_START_DATE_ImageCal" TargetControlID="txt_START_DATE">
          </ajaxToolkit:CalendarExtender>
          <asp:Image ID="txt_START_DATE_ImageCal" runat="server" ImageUrl="~/Images/Calendar_scheduleHS.png" />
          <asp:RegularExpressionValidator ID="txt_START_DATE_REV" runat="server" Display="Dynamic" ErrorMessage="日期格式有誤"
            SetFocusOnError="True" ForeColor="Red" ControlToValidate="txt_START_DATE" ValidationExpression="\d{8}" />
          &nbsp;~ 
           <%--<Kerry:ucDateTimePicker ID="txt_END_DATE" runat="server" />--%>
          <asp:TextBox ID="txt_END_DATE" runat="server"></asp:TextBox>
          <ajaxToolkit:CalendarExtender ID="txt_END_DATE_CalendarExtender" runat="server"
            Enabled="True" Format="yyyyMMdd" PopupButtonID="txt_END_DATE_ImageCal" TargetControlID="txt_END_DATE">
          </ajaxToolkit:CalendarExtender>
          <asp:Image ID="txt_END_DATE_ImageCal" runat="server" ImageUrl="~/Images/Calendar_scheduleHS.png" />
          <asp:RegularExpressionValidator ID="txt_END_DATE_RV" runat="server" Display="Dynamic" ErrorMessage="日期格式有誤"
            SetFocusOnError="True" ForeColor="Red" ControlToValidate="txt_END_DATE" ValidationExpression="\d{8}" />
          &nbsp;</td>
        <td></td>
      </tr>
      <tr>
        <td colspan="2">
          <asp:Button ID="btn_search" runat="server" Text="查詢" OnClick="btn_Query_Click" />
          <asp:Button ID="btn_insert" runat="server" Text="新增" OnClick="btn_Insert_Click" />
          <asp:Label ID="lbl_message" runat="server" EnableViewState="False" ForeColor="Red"></asp:Label>
        </td>
      </tr>

    </table>
    <br />
    <asp:Panel ID="PanelControl" runat="server" ScrollBars="Auto">
      <asp:GridView ID="GridView_BULLETIN" runat="server" AllowPaging="True" RowStyle-Wrap="false" HeaderStyle-Wrap="false"
        AutoGenerateColumns="False" DataKeyNames="BULLETIN_ID"
        DataSourceID="SDS_KRJ_BULLETIN_MASTER"
        OnSelectedIndexChanging="GridView_BULLETIN_SelectedIndexChanging"
        OnRowEditing="GridView_BULLETIN_RowEditing"
        OnRowDeleting="GridView_BULLETIN_RowDeleting" OnPageIndexChanging="GridView_BULLETIN_PageIndexChanging">
        <Columns>
          <asp:TemplateField ShowHeader="False" HeaderText="功能" ItemStyle-Width="150">
            <ItemTemplate>
              <asp:Button ID="btn_view" runat="server" CausesValidation="False" CommandName="Select" Text="檢視" />
              <asp:Button ID="btn_edit" runat="server" CausesValidation="False" CommandName="Edit" Text="修改" />
              <asp:Button ID="btn_delete" runat="server" CausesValidation="False" CommandName="Delete" Text="刪除" />
            </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="BULLETIN_ID" HeaderText="公告代號" ReadOnly="True" SortExpression="BULLETIN_ID" />
          <asp:BoundField DataField="BULLETIN_TEXT_TW" HeaderText="大綱[繁]" SortExpression="BULLETIN_TEXT" />
          <asp:BoundField DataField="BULLETIN_SPEC_TW" HeaderText="內容" SortExpression="BULLETIN_SPEC" Visible="False" />
          <asp:BoundField DataField="BULLETIN_TEXT_GB" HeaderText="大綱[簡]" SortExpression="BULLETIN_TEXT" />
          <asp:BoundField DataField="BULLETIN_SPEC_GB" HeaderText="內容" SortExpression="BULLETIN_SPEC" Visible="False" />
          <asp:BoundField DataField="BULLETIN_TEXT_EN" HeaderText="大綱[英]" SortExpression="BULLETIN_TEXT" />
          <asp:BoundField DataField="BULLETIN_SPEC_EN" HeaderText="內容" SortExpression="BULLETIN_SPEC" Visible="False" />
          <asp:BoundField DataField="START_DATE" HeaderText="公告開始日" SortExpression="START_DATE" />
          <asp:BoundField DataField="END_DATE" HeaderText="公告結束日" SortExpression="END_DATE" />
          <asp:BoundField DataField="STATUS" HeaderText="是否啟用" SortExpression="STATUS" />
          <asp:BoundField DataField="REMARK" HeaderText="註解" SortExpression="REMARK" />
          <asp:BoundField DataField="CRT_ID" HeaderText="CRT_ID" SortExpression="CRT_ID" Visible="False" />
          <asp:BoundField DataField="CRT_DATE" HeaderText="CRT_DATE" SortExpression="CRT_DATE" Visible="False" />
          <asp:BoundField DataField="CRT_TIME" HeaderText="CRT_TIME" SortExpression="CRT_TIME" Visible="False" />
          <asp:BoundField DataField="UPD_ID" HeaderText="UPD_ID" SortExpression="UPD_ID" Visible="False" />
          <asp:BoundField DataField="UPD_DATE" HeaderText="UPD_DATE" SortExpression="UPD_DATE" Visible="False" />
          <asp:BoundField DataField="UPD_TIME" HeaderText="UPD_TIME" SortExpression="UPD_TIME" Visible="False" />
        </Columns>
        <PagerTemplate>
          <Kerry:ucGridViewPager ID="ucPager" runat="server" />
        </PagerTemplate>
      </asp:GridView>
      <asp:SqlDataSource ID="SDS_KRJ_BULLETIN_MASTER" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
        SelectCommand="SELECT BULLETIN_ID, case when length(bulletin_text_tw) > 15 then substr(bulletin_text_tw,0,15) || ' ...' else bulletin_text_tw end BULLETIN_TEXT_TW, BULLETIN_SPEC_TW, case when length(bulletin_text_en) > 15 then substr(bulletin_text_en,0,15) || ' ...' else bulletin_text_en end bulletin_text_en, BULLETIN_SPEC_EN, case when length(bulletin_text_gb) > 15 then substr(bulletin_text_gb,0,15) || ' ...' else bulletin_text_gb end bulletin_text_gb, BULLETIN_SPEC_GB, START_DATE, END_DATE, STATUS, case when length(REMARK) > 15 then substr(REMARK,0,15) || ' ...' else REMARK end REMARK, CRT_ID, CRT_DATE, CRT_TIME, UPD_ID, UPD_DATE, UPD_TIME FROM TG.KRJ_BULLETIN_MASTER WHERE 1=1 "
        OnSelecting="SDS_KRJ_BULLETIN_MASTER_Selecting" OnDeleted="SDS_KRJ_BULLETIN_MASTER_Deleted"></asp:SqlDataSource>
      <br />
    </asp:Panel>
  </div>



</asp:Content>
